//
//  GKZYUserSortViewController.m
//  GKZYAid
//
//  Created by Tony Zhang on 16/3/15.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYUserSortViewController.h"
#import "GKZYSortLeftBarButtonItemView.h"
#import "MyDefine.h"
#import "GKZYUserSortDataHandel.h"
#import "MBProgressHUD.h"

@interface GKZYUserSortViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UILabel *locationLab;
@property(nonatomic,assign)BOOL dropOrPick;
@property(nonatomic,strong)UITableView *locationTableView;
@property(nonatomic,strong)NSArray *provinceArray;
@property(nonatomic,assign)BOOL wenOrLi;
@property(nonatomic,strong)UITextField *scoreTF;

@end

@implementation GKZYUserSortViewController


-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    if (self.dropOrPick) {
    
        self.locationTableView.frame = CGRectMake(KmainScreenBoundsWidth*0.15,  VCHEIGHT*0.15+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.5);
        
    }else{
    
     self.locationTableView.frame = CGRectMake(KmainScreenBoundsWidth*0.15,  VCHEIGHT*0.15+64, KmainScreenBoundsWidth*0.7, 0);
        
    }
    [UIView commitAnimations];
    
}

-(void)showAction:(NSNotification *)aNotification{

    self.view.frame = CGRectMake(0, -150, KmainScreenBoundsWidth, VCHEIGHT);
}


-(void)closeKeyBoard{

    self.view.frame = CGRectMake(0, 0, KmainScreenBoundsWidth, VCHEIGHT);
}


-(void)viewDidLoad{

    [super viewDidLoad];
    self.navigationItem.title = @"考生信息";
    self.view.backgroundColor = [UIColor whiteColor];
    GKZYSortLeftBarButtonItemView *leftBar = [[GKZYSortLeftBarButtonItemView alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    [leftBar addGestureRecognizer:tap1];
    UIBarButtonItem *leftBBI = [[UIBarButtonItem alloc]initWithCustomView:leftBar];
    self.navigationItem.leftBarButtonItem = leftBBI;
    
    NSArray *segmentArray = @[@"理科",@"文科"];
    UISegmentedControl *wenLi = [[UISegmentedControl alloc]initWithItems:segmentArray];
    wenLi.frame = CGRectMake(KmainScreenBoundsWidth*0.15, VCHEIGHT*0.23+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.08);
    wenLi.layer.borderColor = [UIColor orangeColor].CGColor;
    wenLi.layer.borderWidth = 0.5f;
    wenLi.layer.cornerRadius = 5;
    wenLi.tintColor = [UIColor orangeColor];
    wenLi.layer.masksToBounds = YES;
    [wenLi addTarget:self action:@selector(chooseWL:) forControlEvents:(UIControlEventValueChanged)];
    self.wenOrLi = YES;
     wenLi.selectedSegmentIndex = 0;
    if ([SINGLETON.wl isEqualToString:@"wen"]) {
        self.wenOrLi = NO;
        wenLi.selectedSegmentIndex = 1;
    }
    
    [self.view addSubview:wenLi];
    
    self.scoreTF = [[UITextField alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.15, VCHEIGHT*0.36+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.08)];
    self.scoreTF.placeholder = @"请输入考分";
    self.scoreTF.font = [UIFont systemFontOfSize:14];
    self.scoreTF.layer.borderColor = [UIColor orangeColor].CGColor;
    self.scoreTF.layer.borderWidth = 1;
    self.scoreTF.layer.cornerRadius = 5;
    self.scoreTF.delegate = self;
    if (SINGLETON.score != nil) {
        self.scoreTF.text = SINGLETON.score;
    }
    self.scoreTF.layer.masksToBounds = YES;
    [self.view addSubview:self.scoreTF];
    
    
    UILabel *markLab = [[UILabel alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.15, VCHEIGHT*0.47+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.08)];
    markLab.backgroundColor = [UIColor clearColor];
    markLab.text = @"以上信息均为必填项";
    markLab.textAlignment = NSTextAlignmentCenter;
    markLab.textColor = DeepCustomGrayColor;
    markLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:markLab];
    
    UIButton *confirmBut = [[UIButton alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.15, VCHEIGHT*0.55+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.08)];
    [confirmBut setBackgroundColor:[UIColor orangeColor]];
    [confirmBut setTitle:@"确定" forState:(UIControlStateNormal)];
    confirmBut.layer.cornerRadius = 5;
    confirmBut.layer.masksToBounds = YES;
    [confirmBut addTarget:self action:@selector(confirmInfo) forControlEvents:(UIControlEventTouchUpInside)];
    [confirmBut setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.view addSubview:confirmBut];
    
    UIView *locationView = [[UIView alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.15, VCHEIGHT/10+64, KmainScreenBoundsWidth*0.7, VCHEIGHT*0.08)];
    locationView.layer.borderColor = [UIColor orangeColor].CGColor;
    locationView.layer.borderWidth = 1.0f;
    locationView.layer.cornerRadius = 5;
    locationView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showLocationTableView)];
    [locationView addGestureRecognizer:tap2];
    [self.view addSubview:locationView];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.3, VCHEIGHT*0.02, VCHEIGHT*0.04, VCHEIGHT*0.04)];
    image.image = [UIImage imageNamed:@"location"];
    image.userInteractionEnabled = YES;
    [locationView addSubview:image];
    
   self.locationLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(image.frame), 0, KmainScreenBoundsWidth*0.3, VCHEIGHT*0.08)];
   self.locationLab.text = @"北京";
    if (SINGLETON.loc_name != nil) {
        self.locationLab.text = SINGLETON.loc_name;
    }
    self.locationLab.font = [UIFont systemFontOfSize:14];
    self.locationLab.userInteractionEnabled = YES;
    self.locationLab.textColor = [UIColor orangeColor];
   [locationView addSubview:self.locationLab];

    self.locationTableView = [[UITableView alloc]initWithFrame:CGRectMake(KmainScreenBoundsWidth*0.15,  VCHEIGHT*0.15+64, KmainScreenBoundsWidth*0.7, 0)];
    self.locationTableView.dataSource = self;
    self.locationTableView.delegate = self;
    self.locationTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.locationTableView];
    
    self.provinceArray = @[@"北京",@"天津",@"上海",@"重庆",@"河北",@"河南",@"云南",@"辽宁",@"黑龙江",@"湖南",@"安徽",@"山东",@"新疆",@"江苏",@"浙江",@"江西",@"湖北",@"广西",@"甘肃",@"山西",@"内蒙古",@"陕西",@"吉林",@"福建",@"贵州",@"广东",@"青海",@"西藏",@"四川",@"宁夏",@"海南"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAction:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(closeKeyBoard) name:UIKeyboardDidHideNotification object:nil];
    
}



-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)confirmInfo{

    NSString *string = nil;
    if (self.wenOrLi) {
        string = @"li";
    }else{
    string = @"wen";
    }
    [SINGLETON setLocation:self.locationLab.text Score:self.scoreTF.text WenLi:string];
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)showLocationTableView{

    self.dropOrPick = !self.dropOrPick;
    if (self.dropOrPick) {
        self.view.backgroundColor = DeepCustomGrayColor;
    }else{
        self.view.backgroundColor = [UIColor whiteColor];
    }
    [self viewWillAppear:YES];

}


-(void)chooseWL:(UISegmentedControl *)sender{

    if (sender.selectedSegmentIndex == 0) {
        self.wenOrLi = YES;
    }else{
        self.wenOrLi = NO;
    }
   
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if ([string isEqualToString:@"\n"]) {//按下return键
        //这里隐藏键盘，不做任何处理
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  self.provinceArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    cell.textLabel.text = self.provinceArray[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.locationLab.text = self.provinceArray[indexPath.row];
    self.dropOrPick = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self.provinceArray[indexPath.row] isEqualToString:@"北京"]) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"此地区录取政策变化，录取概率误差较大";
        hud.labelFont = [UIFont systemFontOfSize:10];
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:2];

    }
    [self viewWillAppear:YES];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.scoreTF endEditing:YES];

}

@end
